package com.rbac.controller;

import com.apis.role.RoleApis;
import com.common.annotation.Authority;
import com.domain.dto.PageDTO;
import com.domain.dto.role.*;
import com.domain.query.role.RoleQuery;
import com.domain.vo.JsonVO;
import com.rbac.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色控制层")
@RestController
@RequestMapping("role")
public class RoleController implements RoleApis {

    @Autowired
    private IRoleService roleService;

    //查询用户列表
    @Override
    @GetMapping("/query-list")
    @Authority("system:role:list")
    @Operation(summary = "查询角色列表（条件+分页）")
    public JsonVO<PageDTO<RoleDTO>> queryRoleList(@Validated @ParameterObject RoleQuery roleQuery) {
        PageDTO<RoleDTO> rolePage = roleService.queryRoleList(roleQuery);
        return JsonVO.success(rolePage);
    }

    //根据角色id来查询角色
    @Override
    @GetMapping("/query-id")
    @Authority("system:role:query")
    @Operation(summary = "查询角色数据（单条）")
    public JsonVO<RoleDTO> queryRoleByRoleId(Integer roleId){
        return JsonVO.success(roleService.queryRoleByRoleId(roleId));
    }

    //新增用户
    @Override
    @GetMapping("/add")
    @Authority("system:role:add")
    @Operation(summary = "添加角色")
    public JsonVO<Integer> addRole(@Validated @ParameterObject AddRoleDTO addRoleDTO){
        return JsonVO.success(roleService.addRole(addRoleDTO));
    }

    //修改用户
    @Override
    @PutMapping("/modify")
    @Authority("system:role:modify")
    @Operation(summary = "修改角色")
    public JsonVO<Integer> modifyRole(@Validated @ParameterObject ModifyRoleDTO modifyRoleDTO){
        return JsonVO.success(roleService.modifyRole(modifyRoleDTO));
    }

    //删除用户
    @Override
    @DeleteMapping("/remove/{roleIds}")
    @Authority("system:role:remove")
    @Operation(summary = "删除用户（支持批量删除）")
    public JsonVO<Integer> removeRole(@PathVariable List<Integer> roleIds){
        return JsonVO.success(roleService.removeRole(roleIds));
    }

    @Override
    @GetMapping("/query-user-name")
    @Authority("system:role:list")
    @Operation(summary = "查询角色名称列表（角色编号）")
    public JsonVO<List<String>> queryUserNameListByRoleId(Integer roleId) {
        return JsonVO.success(roleService.queryUserNameListByRoleId(roleId));
    }

    @Override
    @GetMapping("/query-permission-name")
    @Authority("system:permission:list")
    @Operation(summary = "查询权限名称列表（角色编号）")
    public JsonVO<List<String>> queryPermissionNameListByRoleId(Integer roleId) {
        return JsonVO.success(roleService.queryPermissionNameListByRoleId(roleId));
    }

    @Override
    @PostMapping("/modify/role-permission")
    @Authority("system:permission:modify")
    @Operation(summary = "修改角色权限（角色编号）")
    public JsonVO<Integer> modifyRolePermission(@Validated @ParameterObject ModifyRolePermissionDTO modifyRolePermissionDTO) {
        return JsonVO.success(roleService.modifyRolePermission(modifyRolePermissionDTO));
    }

    @Override
    @GetMapping("/query/permission-tree")
    @Authority("system:permission:list")
    @Operation(summary = "查询角色权限树")
    public JsonVO<List<PermissionTreeDTO>> getPermissionTree(Integer roleId) {
        return JsonVO.success(roleService.getPermissionTree(roleId));
    }

}


//    @GetMapping("/list")
//    public JsonVO getList(Integer pageNum, Integer pageSize){return roleService.getList(pageNum, pageSize);}
//
//    @DeleteMapping("/delete")
//    public JsonVO delete(Integer id){
//        return roleService.delete(id);
//    }
//
//    @PostMapping("/add")
//    public JsonVO save(@RequestBody @Validated Role role){
//        return roleService.save(role);
//    }
//
//    @PutMapping("/update")
//    public JsonVO update(@RequestBody @Validated({Role.Update.class}) Role role){
//        return roleService.update(role);
//    }
//
//    @PutMapping("/v2/update")
//    public JsonVO updateRole(@RequestBody RoleRolePermissionDto roleDto){
//        return roleService.updateRole(roleDto);
//    }
//    @GetMapping("info")
//    public JsonVO info(Integer id){
//        Role role = roleService.getRoleById(id);
//        return JsonVO.success(role);
//    }
//    @GetMapping("listAll")
//    public JsonVO info(){
//        List<Role> list = roleService.getRoleAllById();
//        return JsonVO.success(list);
//    }
//
//    @GetMapping("/info/userRole")
//    public JsonVO getUserRole(Integer roleId){
//        return roleService.getUserRole(roleId);
//    }
//    @GetMapping("/info/rolePermission")
//    public JsonVO getRolePermission(Integer roleId){
//        return roleService.getRolePermission(roleId);
//    }
//
